package com.renchunlin.taobaounion.ui.activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.renchunlin.taobaounion.R;
import com.renchunlin.taobaounion.base.BaseActivity;
import com.renchunlin.taobaounion.base.BaseFragment;
import com.renchunlin.taobaounion.ui.fragment.HomeFragment;
import com.renchunlin.taobaounion.ui.fragment.OnSellFragment;
import com.renchunlin.taobaounion.ui.fragment.SearchFragment;
import com.renchunlin.taobaounion.ui.fragment.SelectedFragment;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements IMainActivity {

    @BindView(R.id.main_navigation_bar)
    public BottomNavigationView mNavigationView;
    private HomeFragment homeFragment;
    private OnSellFragment redPackFragment;
    private SelectedFragment selectedFragment;
    private SearchFragment searchFragment;
    private FragmentManager fm;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        initFragment();
    }

    @Override
    protected void initEvent() {
        initListener();
    }

    @Override
    protected void initPresenter() {

    }

    private void initFragment() {
        homeFragment = new HomeFragment();
        redPackFragment = new OnSellFragment();
        selectedFragment = new SelectedFragment();
        searchFragment = new SearchFragment();
        fm = getSupportFragmentManager();
        //默认选中首页
        switchFragment(homeFragment);
    }

    private void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        switchFragment(homeFragment);
                        break;
                    case R.id.selected:
                        switchFragment(selectedFragment);
                        break;
                    case R.id.red_packet:
                        switchFragment(redPackFragment);
                        break;
                    case R.id.search:
                        switchFragment(searchFragment);
                        break;
                }
                return true;
            }
        });
    }

    //上一次显示的Fragment
    private BaseFragment lastOneFragment = null;

    private void switchFragment(BaseFragment targetFragment) {
        //如果上一个Fragment跟当前要切换的fragment是同一个，那么不需要切换
        if (lastOneFragment == targetFragment) {
            return;
        }

        //修改成add和hide的方式来控制Fragment
        FragmentTransaction transaction = fm.beginTransaction();
        if (!targetFragment.isAdded()) {
            transaction.add(R.id.main_page_container, targetFragment);
        } else {
            transaction.show(targetFragment);
        }
        if (lastOneFragment != null) {
            transaction.hide(lastOneFragment);
        }
        lastOneFragment = targetFragment;
        transaction.commit();

        //replace方法切换，每次都要重复加载
        /*FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.main_page_container, targetFragment);
        transaction.commit();*/
    }

    /**
     * 跳转到搜索页面
     */
    @Override
    public void switch2Search() {
        mNavigationView.setSelectedItemId(R.id.search);
    }
}